package com.wooophone.file.action;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;

import com.wooophone.common.web.action.BaseAction;
import com.wooophone.file.util.Util;

public class FileDownloadAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private String inputPath;
	
	private String filename;
	
	private static long count=0;

	public InputStream getTargetFile() throws UnsupportedEncodingException {
		
		count++;
		
		inputPath=new String("/files/"+filename);
		
		filename =new String(filename.getBytes("GBK"),"iso-8859-1");
		
		System.out.println("第"+count+"次下载.....");
		
		Map<String, String> map=new HashMap<String, String>();
		
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		
		map.put("index", String.valueOf(count));
		map.put("time", format.format(new Date()));
		map.put("ip", getIpAddr());
		
		System.out.println(getIpAddr());
		
		ServletContext servletContext=ServletActionContext.getServletContext();
		
		InputStream inputStream=servletContext.getResourceAsStream(inputPath);
		
		String url = servletContext.getRealPath("download_log.txt");
		Util.write(url, map);
		
		return inputStream;
	}

	public String getInputPath() {
		return inputPath;
	}
	
	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}
	
}
